<?php
namespace Home\Controller;
use Think\Controller;
use Think\Verify;
class IndexController extends Controller {
    protected function __intialize(){
      //判断用户登录注册状态
    }
    public function index(){
        $this->display('index');
    }
    public function login(){
      $this->show('Helo world!');
    }
    public function register(){
      $this->display('register');
    }
    public function registerForm(){
      if (IS_AJAX) {
        if (IS_POST) {
          $reg_status = 0;
          //构建数据
          $account = I('post.name');
          $password1 = I('post.password1');
          $password2 = I('post.password2');
          $phone = I('post.phone');
          $email = I('post.email');
          $vercode = I('post.code');
          //验证码正确性验证
          $ver_res = $this->check_verify($vercode, 1);
          if ($ver_res) {
            $ver_status = 1;
            $ver_msg = '验证码正确';
            $user = M('user');
            //验证账号名
            if ($account) {
              $account_res = $user->where('account='.$account)->select();
              if ($account_res != []) {
                $account_status = 1;
                $account_msg = '用户名可用';
                //验证用户名通过
              } else{
                $account_status = 0;
                $account_msg = '用户名已被注册，请更换用户名。';
              }
            }
            //验证密码
            if ($password1 === $password2) {
              //密码验证通过
              $psw_status = 1;
              $psw_msg = '两次密码相同，验证通过。';
            } else {
              //密码未通过验证
              $psw_status = 0;
              $psw_msg = '两次密码不相同，请检查后再注册。';
            }
            //验证手机号码
            if ($phone) {
              $phone_res = $user->where('phone='.$phone)->select();
              if ($phone_res != []) {
                //手机验证通过
                $phone_status = 1;
                $phone_msg = '手机号码可用';
              } else {
                //手机号码已注册，请更换手机号码
                $phone_status = 0;
                $phone_msg = '手机号码已被注册，请您更换手机号码。';
              }
            }
            //验证邮箱
            if ($email) {
              $email_res = $user->where('email='.$email)->select();
              if ($email_res != []) {
                //手机验证通过
                $email_status = 1;
                $email_msg = '邮箱可用';
              } else {
                //手机号码已注册，请更换手机号码
                $email_status = 0;
                $email_msg = '邮箱已被注册，请您更换邮箱注册。';
              }
            }
            //根据以上验证结果，给出注册状态码
            if ($account_status === 1 && $psw_status === 1 && ($phone_status === 1 || $email_status === 1)) {
              $reg_status = 1;
              $reg_msg = '恭喜您，注册成功。';
              //写入数据库
            } else {
              $reg_msg = '注册失败，请重试。';
            }
          } else {
            $ver_status = 0;
            $ver_msg = '验证码错误，点击图片刷新验证码。';
          }
        }
      }
      //构建ajax返回数据
      $data = [];
      $data['reg_status'] = $reg_status;
      $data['reg_msg'] = $reg_msg;
      $data['ver_status'] = $ver_status;
      $data['ver_msg'] = $ver_msg;
      $data['account_status'] = $account_status;
      $data['account_msg'] = $account_msg;
      $data['psw_status'] = $psw_status;
      $data['psw_msg'] = $psw_msg;
      $data['phone_status'] = $phone_status;
      $data['phone_msg'] = $phone_msg;
      $data['email_status'] = $email_status;
      $data['email_msg'] = $email_msg;

      $this->ajaxReturn($data);
    }
    public function vercode(){
      $config =    array(
        'fontSize'    =>    30,    // 验证码字体大小
        // 'length'      =>    4,     // 验证码位数
        'useNoise'    =>    false, // 关闭验证码杂点
        'useCurve'    =>    false,
        'useImgBg'    =>    false,
        // 'expire'    =>    600,        
      );
      $Verify = new Verify($config);
      $Verify->entry(1);
    }
    protected function check_verify($code, $id = ''){
      $verify = new Verify();
      return $verify->check($code, $id);
    }
}